package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edh.domain.EdhShoppingAddress;
import com.ruoyi.edh.service.IEdhShoppingAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 易订货-收货地址Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/address")
@Api(description = "易订货-收货地址")
public class EdhShoppingAddressController extends BaseController {
    @Autowired
    private IEdhShoppingAddressService edhShoppingAddressService;

    /**
     * 查询易订货-收货地址列表
     */
    @GetMapping("/list")
    @ApiOperation("易订货-收货地址列表")
    public TableDataInfo list(EdhShoppingAddress edhShoppingAddress) {
        startPage();
        List<EdhShoppingAddress> list = edhShoppingAddressService.selectEdhShoppingAddressList(edhShoppingAddress);
        return getDataTable(list);
    }

    /**
     * 查询易订货-收货地址列表
     */
    @GetMapping("/mylist")
    @ApiOperation("易订货-收货地址列表")
    public TableDataInfo mylist(EdhShoppingAddress edhShoppingAddress) {
        edhShoppingAddress.setCreateBy(SecurityUtils.getUserId() + "");
        startPage();
        List<EdhShoppingAddress> list = edhShoppingAddressService.selectEdhShoppingAddressList(edhShoppingAddress);
        return getDataTable(list);
    }

    /**
     * 查询易订货-收货地址列表
     */
    @GetMapping("/defaultAdress")
    @ApiOperation("易订货-收货地址列表")
    public AjaxResult defaultAdress() {
        EdhShoppingAddress edhShoppingAddress = new EdhShoppingAddress();
        edhShoppingAddress.setSort(0);
        edhShoppingAddress.setCreateBy(SecurityUtils.getUserId() + "");
        List<EdhShoppingAddress> list = edhShoppingAddressService.selectEdhShoppingAddressList(edhShoppingAddress);
        if (list.isEmpty()) {
            return AjaxResult.success(null);
        }
        return AjaxResult.success(list.get(0));
    }


    /**
     * 导出易订货-收货地址列表
     */
    @Log(title = "易订货-收货地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出易订货-收货地址列表")
    public AjaxResult export(EdhShoppingAddress edhShoppingAddress) {
        List<EdhShoppingAddress> list = edhShoppingAddressService.selectEdhShoppingAddressList(edhShoppingAddress);
        ExcelUtil<EdhShoppingAddress> util = new ExcelUtil<EdhShoppingAddress>(EdhShoppingAddress.class);
        return util.exportExcel(list, "address");
    }

    /**
     * 获取易订货-收货地址详细信息
     */
    @GetMapping
    @ApiOperation("获取易订货-收货地址详细信息")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(edhShoppingAddressService.selectEdhShoppingAddressById(id));
    }

    /**
     * 新增易订货-收货地址
     */
    @Log(title = "易订货-收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增易订货-收货地址")
    public AjaxResult add(@RequestBody EdhShoppingAddress edhShoppingAddress) {
        return toAjax(edhShoppingAddressService.insertEdhShoppingAddress(edhShoppingAddress));
    }

    /**
     * 修改易订货-收货地址
     */
    @Log(title = "易订货-收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改易订货-收货地址")
    public AjaxResult edit(@RequestBody EdhShoppingAddress edhShoppingAddress) {
        return toAjax(edhShoppingAddressService.updateEdhShoppingAddress(edhShoppingAddress));
    }

    /**
     * 删除易订货-收货地址
     */
    @Log(title = "易订货-收货地址", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    @ApiOperation("删除易订货-收货地址")
    public AjaxResult remove(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return AjaxResult.error("请选择收货地址");
        }
        return toAjax(edhShoppingAddressService.deleteEdhShoppingAddressByIds(ids.split(",")));
    }
}
